package cn.topcodes.tcsf.app.web.controller.admin;

import cn.topcode.unicorn.utils.ValidationUtil;
import cn.topcodes.tcsf.admin.commons.api.controller.admin.AdminController;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.domain.entity.SysMenu;
import cn.topcodes.tcsf.admin.domain.form.NestableSort;
import cn.topcodes.tcsf.app.domain.entity.HelpArticleCatalog;
import cn.topcodes.tcsf.app.domain.form.HelpArticleCatalogForm;
import cn.topcodes.tcsf.app.domain.vo.HelpArticleCatalogVo;
import cn.topcodes.tcsf.app.mapper.HelpArticleCatalogMapper;
import cn.topcodes.tcsf.app.service.HelpArticleCatalogService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 类别管理控制器
 *
 * @author Yvan
 */
@Controller
@RequestMapping("/admin/helparticlecatalog")
public class HelpArticleCatalogController extends AdminController<HelpArticleCatalog, HelpArticleCatalogVo, HelpArticleCatalogForm> {

    @Autowired
    private HelpArticleCatalogService helpArticleCatalogService;

    @Autowired
    private HelpArticleCatalogMapper helpArticleCatalogMapper;

    @Override
    protected String getPageFolder() {
        return "app/admin/helparticlecatalog";
    }


    @Override
    public String create(HttpServletRequest request, Model model) {

        List<HelpArticleCatalog> helpArticleCatalogs = helpArticleCatalogService.findAll();
        model.addAttribute("data", helpArticleCatalogs);
        return this.getPageFolder() + "/create";
    }

    @RequestMapping(value = "listData")
    @ResponseBody
    public ApiResponseEntity<List<HelpArticleCatalogVo>> listData(HttpServletRequest request) {
        Map<String, String[]> searchParams = request.getParameterMap();
        List<HelpArticleCatalog> listT = new ArrayList<>();
        HelpArticleCatalog helpArticleCatalog = new HelpArticleCatalog();
        helpArticleCatalog.setName("根节点");
        helpArticleCatalog.setId(0L);
        listT.add(helpArticleCatalog);
        listT.addAll(helpArticleCatalogService.findAll(searchParams));
        List<HelpArticleCatalogVo> listV = this.entity2Vo(listT, request);
        ApiResponseEntity<List<HelpArticleCatalogVo>> apiResponse = ApiResponseEntity.buildOK();
        return apiResponse.setBody(listV);
    }

    @RequestMapping("save-sort")
    @ResponseBody
    public ApiResponseEntity saveSort(@RequestBody(required = true) List<NestableSort> sort) {

        helpArticleCatalogService.saveSort(sort);
        return ApiResponseEntity.buildOK();
    }

    @Override
    public ApiResponseEntity create(HttpServletRequest request, @Valid HelpArticleCatalogForm form, BindingResult result) {

        ValidationUtil.validate(result);
        HelpArticleCatalog entity = new HelpArticleCatalog();
        entity.setName(form.getName());
        entity.setParentId(null);
        entity.setSort(1);
        Long id = helpArticleCatalogService.create(entity);
        return ApiResponseEntity.buildOK().setBody(id);
    }

    @Override
    public ApiResponseEntity<Void> update(HttpServletRequest request, @Valid @ApiParam("更新资源表单") HelpArticleCatalogForm form, BindingResult result) {
        ValidationUtil.validate(result);
        HelpArticleCatalog t = this.form2Entity(form);
        HelpArticleCatalog entity = this.getService().findOne(form.getId());
        if(entity == null)
            throw new EntityNotFoundException("无法修改！菜单不存在");
        t.setParentId(entity.getParentId());
        t.setSort(entity.getSort());
        helpArticleCatalogService.update(t);
        return ApiResponseEntity.buildOK();
    }

    @Override
    protected void entity2VoDiff(HelpArticleCatalog entity, HelpArticleCatalogVo vo) {
        if (null != entity.getParentId()) {
            HelpArticleCatalog one = this.helpArticleCatalogService.findOne(entity.getParentId());
            vo.setParentNote(one == null ? "" : one.getName());
        } else {
            vo.setParentNote("根节点");
        }
        super.entity2VoDiff(entity, vo);
    }

    //    @Override
//    protected HelpArticleCatalogVo entity2Vo(HelpArticleCatalog entity, HttpServletRequest request) {
//        HelpArticleCatalogVo helpArticleCatalogVo = new HelpArticleCatalogVo();
//        helpArticleCatalogVo.setId(entity.getId());
//        helpArticleCatalogVo.setName(entity.getName());
//        if (null != entity.getParentId()) {
//            HelpArticleCatalog catalogServiceOne = helpArticleCatalogService.findOne(entity.getParentId());
//            helpArticleCatalogVo.setParentNode(catalogServiceOne == null ? "" : catalogServiceOne.getName());
//
//        }
//        return helpArticleCatalogVo;
//    }
}
